#include "PTableWidget.h"

PTableWidget::PTableWidget(QWidget *parent) : QTableWidget( parent )
{
    loadData();
}

int PTableWidget::loadData()
{
    return LoadDataFromFile( "algorithms.tfs" );
}

int PTableWidget::LoadDataFromFile( const QString& fileName )
{
    QFile file(fileName);
    if (!file.open( QFile::ReadOnly )) return 1;

    QDataStream in( &file );

    //reading table size
    int n, m;
    in >> n >> m;
    setRowCount( n );
    setColumnCount( m );

    //reading column headers
    QString str;
    for (int i=0; i<m; i++){
        in >> str;
        setHorizontalHeaderItem( i, new QTableWidgetItem( str ) );
    }

    //reading items list
    items.clear();
    itemType item( m );
    for (int i=0; i<n; i++){
        in >> item;
        items.push_back( item );
        itemNames[ item.getField(0) ] = true;
    }

    //full filling table
    updateTable();

    file.close();
    return 0;
}

int PTableWidget::saveData()
{
    return SaveDataToFile( "algorithms.tfs" );
}

int PTableWidget::SaveDataToFile( const QString& fileName)
{
    QFile file(fileName);
    if (!file.open( QFile::WriteOnly )) return 1;

    QDataStream out( &file );

    //writing table size
    out << rowCount() << columnCount();

    //writing column headers
    QString t;
    QTableWidgetItem *wi;
    for (int i=0; i<columnCount(); i++){
        wi = horizontalHeaderItem( i );
        if (wi == 0) t = ""; else t = wi->text();
        out << t;
    }

    //forming itemType elements
    items.clear();
    itemType item( columnCount() );
    for (int i=0; i<rowCount(); i++){
        for (int j=0; j<columnCount(); j++){
            wi = this->item( i, j );
            if (wi == 0) t = ""; else t = wi->text();
            item.setField( j, t );
        }

        items.push_back( item );
    }
    //writing items list
    for (int i=0; i<items.size(); i++)
        out << items[i];

    file.close();
    return 0;
}

void PTableWidget::getItems( QVector<itemType>& items )
{
    items.clear();
    items = this->items;
}

void PTableWidget::updateTable()
{
    setRowCount( items.size() );
    for (int i=0; i<items.size(); i++)
        for (int j=0; j<columnCount(); j++)
            setItem( i, j, new QTableWidgetItem( items[i].getField( j ) ) );

    resizeColumnsToContents();
    resizeRowsToContents();
}

int PTableWidget::insertItem( itemType& item )
{
    /*if (itemNames.find( item.getField(0) ) != itemNames.end() ){
        items.push_front( item );
        itemNames[item.getField(0)] = true;
        updateTable();
        return 0;
    } else return 1;*/
    items.push_front( item );
    updateTable();
    return 0;
}

int PTableWidget::editItem( int row, itemType &item )
{
    if ( row >=0 && row < items.size() ){
        items[row] = item;
        updateTable();
        return 0;
    } else return 2;
}

int PTableWidget::deleteItem( int row )
{
    if ( row >=0 && row < items.size() ){
        items.remove( row, 1 );
        updateTable();
        return 0;
    } else return 2;
}


